package com.qfjy.java2102.ServiceImpl;

import com.qfjy.java2102.bean.ResponseBody;
import com.qfjy.java2102.Interface.KeyInterface;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Service
public class LoginServiceImpl {
    @Value("${spring.login.password}")
    private String password;
    @Value("${spring.login.username}")
    private String username;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource(
            name = "redisTemplate"
    )
    private ValueOperations<String,String> string;
    @Resource(
            name = "redisTemplate"
    )
    private HashOperations<String,String ,Object> hash;
    public ResponseBody checkUserAndCount(String username, String password, String ip) {
        if(hash.hasKey(KeyInterface.key+ip,KeyInterface.keyName)){
            if(this.username.equals(username)){
                if(this.password.equals(password)){
                    hash.put(KeyInterface.key+ip, KeyInterface.count,"0");
                    return ResponseBody.success();
                }else {
                    hash.increment(KeyInterface.key+ip,KeyInterface.count,1);
                    int count= (int) hash.get(KeyInterface.key+ip,KeyInterface.count);
                    if(count>5){
                        redisTemplate.expire(KeyInterface.key+ip,20, TimeUnit.MINUTES);
                        return ResponseBody.failureCount();
                    }else {
                        return ResponseBody.failurePassword();
                    }
                }
            }else {
                return ResponseBody.failureUsername();
            }
        }else {
            //如果redis没有数据，查mysql
            //如果mysql中没有查到账号信息，就跳转到注册页面
            //如果查到查到账号信息，比对信息并存入redis
            //模拟已经查到
            if(this.username.equals(username)){
                if(this.password.equals(password)){
                    hash.put(KeyInterface.key+ip,KeyInterface.keyName,username);
                    hash.put(KeyInterface.key+ip,KeyInterface.keyPwd,password);
                    hash.put(KeyInterface.key+ip,KeyInterface.count,"0");
                    redisTemplate.expire(KeyInterface.key+ip,2, TimeUnit.MINUTES);
                    return ResponseBody.success();
                }else {
                    hash.put(KeyInterface.key+ip,KeyInterface.keyName,username);
                    hash.put(KeyInterface.key+ip,KeyInterface.keyPwd,password);
                    hash.increment(KeyInterface.key+ip,KeyInterface.count,1);
                    redisTemplate.expire(KeyInterface.key+ip,2,TimeUnit.MINUTES);
                    return ResponseBody.failurePassword();
                }

            }else {
                return ResponseBody.failureUsername();
            }

        }
    }
}

